$(function(argument) {
	var user_id = window.localStorage.getItem("user_id");
	var hasOrder = window.sessionStorage.getItem("hasOrder");
	// 获取address-id
	var address_id;
	//console.log(user_id);
	// 获取地址数据
	if (user_id == null) {
		window.location.href = "myself.html";
	} else {
		getAddress();

		function getAddress() {
			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppAddress/getAddressList',
				type: 'POST',
				dataType: 'json',
				data: {
					user_id: user_id
				},
				success: function(data) {
					console.log(data);
					if (data.sing.length == 0) {
						$(".adressList").html('<h2>请添加收货地址</h2>');
					} else {
						var strA = template("myA", {
							myA: data
						});
						$(".adressList ul").html(strA);
					}
				}
			})
		}



		// 设为默认该样式
		$(document).on("click", ".radioT", function() {

			$("label span").html("设为默认").css({
				color: "#666"
			}).parents("li").removeClass('bb');
			$(this).next("span").html("默认地址").css({
				color: "#e8383b"
			}).parents("li").addClass('bb');
			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppAddress/setDefault',
				type: 'POST',
				dataType: 'json',
				data: {
					user_id: user_id,
					address_id: $(this).parents("li").attr('data-id')
				},
				success: function(data) {}
			})
		});

		// 删除
		$(document).on("click", ".del", function(e) {
			e.preventDefault();
			$(".areYouSure").css({
				display: 'block'
			});
			var that = $(this);
			$(".sureS").click(function() {
				$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/AppAddress/delAddress',
					type: 'POST',
					dataType: 'json',
					data: {
						user_id: user_id,
						address_id: that.parents("li").attr('data-id')
					},
					success: function(data) {
						console.log(data)
						getAddress();
						$(".areYouSure").css({
							display: 'none'
						});
					}
				})
			})

		})
		$(".cancel").click(function() {
				$(".areYouSure").css({
					display: 'none'
				});
			})
			// 编辑
		$(document).on("click", ".edit", function(e) {
				e.preventDefault();
				window.sessionStorage.setItem("address_id", $(this).parents("li").attr('data-id'))
				window.location.href = "editAddress.html";
			})
			// 添加地址
		$(".add").click(function(e) {
				e.preventDefault();
				//window.sessionStorage.setItem("user_id", user_id);
				window.location.href = "editAddress.html"
			})
			// 从提交订单过来时选地址，
		if (hasOrder == 1) {
			$(document).on("click", "li", function() {
				var changeAddress = 1;
				window.sessionStorage.setItem("consignee", $(this).find(".userName").html());
				window.sessionStorage.setItem("phoneNumber", $(this).find(".phoneNumber").html());
				window.sessionStorage.setItem("userAdress", $(this).find(".userAdress").html());
				window.sessionStorage.setItem("changeAddress", changeAddress);
				window.sessionStorage.removeItem("hasOrder");
				window.location.href = "order_detail.html";
			})
		}
	}
})